import sys
sys.path.append("/root/program/phantom/")
import tornado.web
import tornado.ioloop
import json

from pytorch.predict.torchserver import TorchPredict

torch_predict = TorchPredict()


class TorchPredictHandler(tornado.web.RequestHandler):
    """主路由处理类"""

    def __init__(self, application, request, **kwargs):
        super().__init__(application, request, **kwargs)
        self.torch_predict = torch_predict

    def post(self):  # 我们修改了这里
        """对应http的post请求方式"""
        req_body = json.loads(str(self.request.body, encoding='utf-8'), strict=False)
        print("#######", req_body)
        output = self.torch_predict.predict(req_body)
        print(output, "******")
        print(json.dumps(output, ensure_ascii=False))
        self.write(json.dumps(output, ensure_ascii=False))


if __name__ == "__main__":
    app = tornado.web.Application([
        (r"/phantom/torch-predict", TorchPredictHandler),
    ])
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()
